package cn.iocoder.yudao.module.crm.controller.admin.statistics;

import cn.hutool.core.map.MapUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.crm.controller.admin.business.vo.business.CrmBusinessPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.customer.vo.customer.CrmCustomerPageReqVO;
import cn.iocoder.yudao.module.crm.controller.admin.statistics.vo.CrmStatisticsIndexReqVO;
import cn.iocoder.yudao.module.crm.dal.dataobject.business.CrmBusinessDO;
import cn.iocoder.yudao.module.crm.service.clue.CrmClueService;
import cn.iocoder.yudao.module.crm.service.customer.CrmCustomerService;
import cn.iocoder.yudao.module.crm.service.statistics.CrmStatisticsIndexService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;


@Tag(name = "管理后台 - 首页crm客资统计")
@RestController
@RequestMapping("/crm/statistics-index")
@Validated
public class CrmStatisticsIndexController {


    @Resource
    private CrmStatisticsIndexService indexService;


    @GetMapping("/customer-count")
    @Operation(summary = "销售简报-客户数据统计")
    @OperateLog(enable = false)
    public CommonResult<Map<String, Long>> customerCount(@Valid CrmStatisticsIndexReqVO indexReqVO) {
        indexReqVO.setUserIds(indexService.getUserIds(indexReqVO));
        Long ownCustomerCount = indexService.getOwnCustomerCountByUserByDate(indexReqVO);
        Long partCustomerCount = indexService.getPartCustomerCountByUserByDate(indexReqVO);
        Long followedCustomerCount = indexService.getFollowedCustomerCountByUserByDate(indexReqVO);
        Long dealCustomerCount = indexService.getDealCustomerCountByUserByDate(indexReqVO);
        Map<String, Long> result = MapUtil.<String,Long>builder()
                .put("ownCustomerCount", ownCustomerCount)
                .put("partCustomerCount", partCustomerCount)
                .put("followedCustomerCount", followedCustomerCount)
                .put("dealCustomerCount", dealCustomerCount)
                .build();

        return success(result);
    }


    @GetMapping("/clue-count")
    @Operation(summary = "销售简报-线索数据统计")
    @OperateLog(enable = false)
    public CommonResult<Map<String, Long>> clueCount(@Valid CrmStatisticsIndexReqVO indexReqVO) {
        indexReqVO.setUserIds(indexService.getUserIds(indexReqVO));
        Long ownClueCount = indexService.getOwnClueCountByUserByDate(indexReqVO);
        Long partClueCount = indexService.getPartClueCountByUserByDate(indexReqVO);
        Long followedClueCount = indexService.getFollowedClueCountByUserByDate(indexReqVO);
        Long transClueCount = indexService.getTransClueCountByUserByDate(indexReqVO);
        Map<String, Long> result = MapUtil.<String,Long>builder()
                .put("ownClueCount", ownClueCount)
                .put("partClueCount", partClueCount)
                .put("followedClueCount", followedClueCount)
                .put("transClueCount", transClueCount)
                .build();

        return success(result);
    }

    @GetMapping("/business-count")
    @Operation(summary = "销售简报统计")
    @OperateLog(enable = false)
    public CommonResult<Map<String, Object>> getBusinessCount(@Valid CrmStatisticsIndexReqVO indexReqVO) {
        Map<String, Object> result = new HashMap<>();
        indexReqVO.setUserIds(indexService.getUserIds(indexReqVO));
        Long businessCount = indexService.getBusinessCount(indexReqVO);
        BigDecimal businessAmountSum = indexService.getBusinessAmount(indexReqVO);
        result.put("businessCount", businessCount);
        result.put("businessAmountSum", Objects.isNull(businessAmountSum) ? 0 : businessAmountSum);

        Long contractCount = indexService.getContractCount(indexReqVO);
        BigDecimal contractAmountSum = indexService.getContractAmount(indexReqVO);
        result.put("contractCount", contractCount);
        result.put("contractAmountSum", Objects.isNull(contractAmountSum) ? 0 : contractAmountSum);


        return success(result);
    }
}
